app.controller('course', ['$scope','$modal','$http','$rootScope','$state','$sessionStorage', function($scope,$modal,$http,$rootScope,$state,$sessionStorage){
	// 获取分类列表
	$scope.getList=function(){
		$http({
		    url:$rootScope.baseUrl+"Course/getCourseTheme",
		    method:"post",
		}).then(function(res){
		    if(res.data.code==200){
		    	$scope.getCourseThemeList=res.data.data;

		    	var all={
		    	    name:"全部分类"
		    	}
		    	$scope.getCourseThemeList.unshift(all);
		    	$scope.courseTheme=$scope.getCourseThemeList[0];
		    }else if(res.data.code==409){
		    	$state.go("access.signin");
		    }
		},function(error){
		    console.log(error);
		});
	}
	$scope.getList();
	
	$scope.selectCourseTheme=function(item){
		$scope.courseTheme=item;
		$scope.getCourseList();
	}

	// 获取课程列表
	$scope.getCourseList=function(){
		var data={
			page:1
		}

		if($scope.courseTheme){
			if($scope.courseTheme.id){
				data.course_theme_id=$scope.courseTheme.id;
			}
		}

		if($scope.keyword){
			data.name=$scope.keyword;
		}
		
		$http({
		    url:$rootScope.baseUrl+"Course/getCourseList",
		    method:"post",
		    data:data
		}).then(function(res){
		    if(res.data.code==200){
		    	$scope.courseList=res.data.data.list;

		    	if($scope.courseList.length==0){
		    		$scope.noData=true;
		    	}else {
		    		$scope.noData=false;
		    	}

		    	// 显示分页
		    	var total=res.data.data.total;
		    	createPage(10,8,total);//每页15条数据，显示8个按钮
		    	function createPage(pageSize, buttons, total) {
		    	    $(".pagination").jBootstrapPage({
		    	        pageSize : pageSize,
		    	        total : total,
		    	        maxPageButton:buttons,
		    	        onPageClicked: function(obj, pageIndex) {
		    	            data.page=pageIndex+1;
		    	            $http({
		    	                method:"post",
		    	                url:$rootScope.baseUrl+"Course/getCourseList",
		    	                data:data
		    	            }).then(function(res){
		    	                if(res.data.code==200){
		    	                	$scope.courseList=res.data.data.list;

		    	                	if($scope.courseList.length==0 && Math.ceil(total/15)==1){
		    	                		$scope.noData=true;
		    	                	}else {
		    	                		$scope.noData=false;
		    	                	}
		    	                }
		    	            },function(error){
		    	                console.log(error);
		    	            });
		    	        }
		    	    });
		    	}
		    }else if(res.data.code==409){
		    	$state.go("access.signin");
		    }
		},function(error){
		    console.log(error);
		});
	}
	$scope.getCourseList();

	$scope.deleteCourse=function(item){
		swal({
		    title: "确定删除该课程？",
		    type: "warning",
		    showCancelButton: true,
		    confirmButtonColor: "#619ce1",
		    confirmButtonText: "确定",
		    closeOnConfirm: false
		},
		function() {
		    $http({
		        url:$rootScope.baseUrl+"Course/delCourse",
		        method:"post",
		        data:{
		        	course_id:item.id
		        }
		    }).then(function(res){
		        if(res.data.code==200){
		        	swal("删除成功", "", "success");
		        	$scope.getCourseList();
		        }else if(res.data.code==409){
			    	$state.go("access.signin");
			    }
		    },function(error){
		        console.log(error);
		    });
		});
	}

	// 编辑课程
	$scope.editCourse=function(item){
		$sessionStorage.courseId=item.id;
		$state.go("fullScore.editCourse");
	}

	// 修改审核状态
	$scope.examine=function(item){
		$http({
		    url:$rootScope.baseUrl+"Course/changeCourseStatus",
		    method:"post",
		    data:{
		    	id:item.id,
		    	status:item.status
		    }
		}).then(function(res){
		    if(res.data.code==200){
		    	if(item.status==0){
		    		item.status=1;
		    	}else {
		    		item.status=0;
		    	}
		    }else if(res.data.code==409){
		    	$state.go("access.signin");
		    }
		},function(error){
		    console.log(error);
		});
	}
}]);